//
//  SEGSelectYearView.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2019/3/4.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGSelectYearView.h"

@interface SEGSelectYearView () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView *pickerView;
@end

@implementation SEGSelectYearView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = kColor_bg3;
    self.pickerView = [[UIPickerView alloc] init];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.backgroundColor = kColor_W;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy";
    NSInteger currentYear = [[formatter stringFromDate:[NSDate date]] integerValue];
    if (currentYear < 2050) {
        [self.pickerView selectRow:currentYear - 1950 inComponent:0 animated:NO];
    }
    [self addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(250);
    }];
    
    UILabel *titleLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = kFont_16;
        lable.text = @"选择年份";
        lable.textAlignment = NSTextAlignmentCenter;
        lable.backgroundColor = kColor_W;
        lable;
    });
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.pickerView.mas_top);
        make.height.mas_equalTo(44);
    }];
    
    UIButton *comfirmButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:kColor_T forState:UIControlStateNormal];
        button.titleLabel.font = kFont_16;
        [button addTarget:self action:@selector(comfirmButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button;
    });
    [comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_top);
        make.bottom.equalTo(titleLabel.mas_bottom);
        make.right.mas_equalTo(-15);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColor_L3;
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(titleLabel.mas_bottom);
        make.height.mas_equalTo(1);
    }];
}

- (void)comfirmButtonTap:(UIButton *)sender {
    if (self.comfirmBlock) {
        NSInteger selectedRow = [self.pickerView selectedRowInComponent:0];
        self.comfirmBlock(1950 + selectedRow);
    }
    
    [self dismiss];
}

- (void)dismiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UIPickerViewDelegate methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 101;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat:@"%ld年", (long)(1950 + row)];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return  38;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}

@end
